package com.ebo.httpcall4j.spring.autoconfig;

import com.ebo.httpcall4j.core.log.HttpCallLogService;
import com.ebo.httpcall4j.spring.HttpCallContentCachingFilter;
import com.ebo.httpcall4j.spring.LogIncomingCallAspect;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 * httpcall4j 入站请求日志的自动配置类。
 * 负责注册 LogIncomingCallAspect 切面和 HttpCallContentCachingFilter 过滤器。
 * @author Ebo
 */
@Configuration
@EnableAspectJAutoProxy
public class HttpCallInboundAutoConfiguration {

    /**
     * 创建 LogIncomingCallAspect 的 Bean。
     *
     * @return LogIncomingCallAspect 的 Bean 实例
     */
    @Bean
    public LogIncomingCallAspect logIncomingCallAspect() {
        return new LogIncomingCallAspect();
    }

    /**
     * 注册 HttpCallContentCachingFilter 过滤器。
     *
     * @return HttpCallContentCachingFilter 的 FilterRegistrationBean 实例
     */
    @Bean
    public FilterRegistrationBean<HttpCallContentCachingFilter> httpCallContentCachingFilter() {
        FilterRegistrationBean<HttpCallContentCachingFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new HttpCallContentCachingFilter());
        registrationBean.addUrlPatterns("/*");
        registrationBean.setOrder(Integer.MAX_VALUE); // 确保该过滤器在其他过滤器之前执行
        return registrationBean;
    }

    /**
     * 创建 HttpCallWebMvcConfigurer 的 Bean。
     *
     * @param logService HttpCallLogService 日志服务
     * @return HttpCallWebMvcConfigurer 的 Bean 实例
     */
    @Bean
    public HttpCallWebMvcConfigurer httpCallWebMvcConfigurer(HttpCallLogService logService) {
        return new HttpCallWebMvcConfigurer(logService);
    }
}